﻿using LWH;
using LWH.ByteProtocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ByteProtocolCSharpDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        SimpleProtocolFactory protocolFacotry = new SimpleProtocolFactory();
        CircularByteBuffer dataQueue = new CircularByteBuffer();

        private void parseProtocol_Click(object sender, EventArgs e)
        {
            //表示接收到数据，并将数据放入数据池
            byte[] receiveData = new byte[] { 0xF7, 0x00, 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a };
            dataQueue.Enqueue(receiveData, 0, receiveData.Length);

            //开始解析协议，解析成功则调用委托
            protocolFacotry.parseProtocol(dataQueue, (protocol) =>
            {
                //当协议解析成功，则调用此委托，可以从中获取协议的各种属性
                var simplePrototol = protocol as SimpleProtocol;
                var header = simplePrototol.getHeaderProperty().getData();
                short length = simplePrototol.getLengthProperty().getValue();
                var mode = simplePrototol.getModeProperty().getData();
            });
        }

        private void buildProtocol_Click(object sender, EventArgs e)
        {
            //创建一个SimpleProtocol，创建一个缓存数组，最简单的使用
            SimpleProtocolBuilder protocolBuilder = new SimpleProtocolBuilder();
            protocolBuilder.getModeProperty().setData(new byte[] { 0x01 }, 0, 1);
            var writeProtocolBytes = protocolBuilder.getData();
            var writeProtocol = protocolBuilder.getProtocol();


            //创建一个长度不定的协议，协议的数据包含传入的文字
            IndefiniteLengthProtocolBuilder stringProtocolBuilder = new IndefiniteLengthProtocolBuilder();
            stringProtocolBuilder.StringProperty.setValue("苟利国家生死以，岂因祸福避趋之。垂死病中惊坐起，谈笑风生又一年。");
            var stringProtocol = stringProtocolBuilder.getProtocol();
        }
    }
}
